#ifndef	_VSPHERE_HANDLE_H
#define	_VSPHERE_HANDLE_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jansson.h"
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>
#include <unordered_map>

#include "types.h"

#define SECTORS_PER_CALL 1024
#define DEFAULT_SECTOR_SIZE 512

#define CBT_DISKCHANGEINFO_KEY "DiskChangeInfo"
#define CBT_DISK_SIZE "length"
#define CBT_CHANGEDAREA_KEY "changedArea"
#define CBT_CHANGEDAREA_START_KEY "start"
#define CBT_CHANGEDAREA_LENGTH_KEY "length"

struct runtime_disk_info_encoding
{
  uint32_t start_magic;
  uint32_t protocol_version;
  uint64_t absolute_disk_length;
  uint64_t absolute_start_offset;
  uint32_t bios_cylinders;
  uint32_t bios_heads;
  uint32_t bios_sectors;
  uint32_t phys_cylinders;
  uint32_t phys_heads;
  uint32_t phys_sectors;
  uint64_t phys_capacity;
  uint32_t adapter_type;
  uint32_t padding[16];
  uint32_t end_magic;
};

struct runtime_meta_data_encoding
{
  uint32_t start_magic;
  uint32_t meta_key_length;
  uint32_t meta_data_length;
  uint32_t end_magic;
};

struct runtime_cbt_encoding
{
  uint32_t start_magic;
  uint64_t start_offset;
  uint64_t offset_length;
  uint32_t end_magic;
};

void process_json_work_file(const char *json_work_file, json_t **json_config);
off_t get_length(json_t *json_config);
std::vector<DataSegment> get_cbtinfo(json_t *json_config);
void read_vsphere_disk_data(std::vector<int>& fd_vector, char *buf, off_t offset, size_t rsize, const std::unordered_map<int, std::vector<DataSegment>>& cache);
void read_vsphere_metadata_data(int fd, char *buf);

#endif